﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc.Html;
using TjZheng.Framework;
using System.Web.Mvc;

namespace TjZheng.EasyUI
{
    public static partial class EasyUIExtension
    {
        /// <summary>
        /// EasyUI文本框，验证时间
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name">input的name属性</param>
        /// <param name="value">input的value属性</param>
        /// <param name="htmlAttributes">html属性</param>
        /// <param name="validTypes">EasyUI提供的验证类型或扩展类型</param>
        /// <returns></returns>
        public static MvcHtmlString DateTimeBox(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes = null, params string[] validTypes)
        {
            IDictionary<string, object> objHtmlAttributes = htmlAttributes.CreateValueDictionary();
            if (objHtmlAttributes.IsNull())
            {
                objHtmlAttributes = new Dictionary<string, object>();
            }
            object data = null;
            List<string> dataOptionsList = new List<string>();
            if (validTypes.IsNoNull())
            {
                dataOptionsList.Add(string.Format("validType:[{0}]", validTypes.JoinComma()));
            }
            if (objHtmlAttributes.TryGetValue("dataoptions", out data))
            {
                if (data.ToString().IsNoNull())
                {
                    dataOptionsList.Add(data.ToString());
                }
                objHtmlAttributes.Remove("dataoptions");
            }
            dataOptionsList.Add("formatter:FormatDate");
            objHtmlAttributes["dataoptions"] = dataOptionsList.JoinComma();
            //dataOptions["parser"] = "ParserDate";
            DateTime objDateTime = DateTime.Now;
            DateTime.TryParse(Convert.ToString(value), out objDateTime);
            return htmlHelper.TextBox("easyui-datetimebox", name, objDateTime, false, string.Empty, objHtmlAttributes, validTypes);
        }
    }
}
